package com.alibaba.citrus.cr.mn.order.center.forecastorder.event;

import com.alibaba.citrus.cr.mn.order.center.forecastorder.service.ForecastOrderBizWriteService;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.crforecastsales.bo.ForecastOrderBO;
import com.epoch.app.mnordercenter.model.dto.ForecastCreateEventRequest;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@EnableAsync
public class ForecastCreateListener implements ApplicationListener<ForecastCreateEvent> {
    private static final Log log = Log.getLogger(ForecastCreateListener.class);

    @Resource
    private ForecastOrderBizWriteService forecastOrderBizWriteService;

    @Async("eventTaskExecutor")
    @Override
    public void onApplicationEvent(ForecastCreateEvent event) {
        ForecastCreateEventRequest source = (ForecastCreateEventRequest) event.getSource();
        log.info("预报订单创建事件入参:{}", JSON.toJSONString(source));
        List<ForecastOrderBO> createForecastOrderSDOS = source.getCreateForecastOrderBOS();
        String fromSys = source.getFromSys();
        String businessUnitCode = source.getBusinessUnitCode();
        String taskId = source.getTaskId();
        forecastOrderBizWriteService.batchCreate(createForecastOrderSDOS, taskId, fromSys, businessUnitCode);
    }
}
